<script setup>
import api from '@/api/index.js'
import { message } from 'ant-design-vue'
import { computed, ref, h, provide } from 'vue'
import MyTable from '@/components/MyTable/MyTable.vue'
import { Modal } from 'ant-design-vue'
import ImageView from '@/components/ImageView/ImageView.vue'
import { getConsants } from '@/utils'

const props = defineProps({
  // 表格数据总条数
  total: { type: Number, default: 0 },
  // 表格当前页码
  pageNum: { type: Number, default: 1 }
})
// 分页参数
const pagination = computed(() => ({
  total: props.total,
  showSizeChanger: false,
  current: props.pageNum,
  onChange: (e) => {
    emits('pageChange', e)
  }
}))

const emits = defineEmits(['edit', 'pageChange', 'reload', 'xq'])

// 表格列配置
const columns = ref([
  { ellipsis: false, title: '培训标题', dataIndex: 'trainTitle', key: 'trainTitle' },
  { ellipsis: false, title: '培训类型', key: 'trainType' },
  { ellipsis: false, title: '培训图片', key: 'trainImage' },
  { ellipsis: false, title: '联系人', dataIndex: 'contactUser', key: 'contactUser' },
  { ellipsis: false, title: '联系电话', dataIndex: 'contactPhone', key: 'contactPhone' },
  { ellipsis: true, title: '培训内容', key: 'trainContent' },
  { ellipsis: false, title: '培训状态', key: 'trainStatus' },
  { ellipsis: false, title: '培训时间', key: 'pickDate' },
  { ellipsis: false, title: '备注', dataIndex: 'remark', key: 'remark' },
  { ellipsis: false, title: '操作', key: 'set', width: '260px', align: 'center' }
])

// 编辑数据
const edit = (item) => {
  emits('edit', item)
}
const xq = (item) => {
  emits('xq', item)
}

//删除数据
const del = (item) => {
  Modal.warning({
    title: '删除培训',
    content: h(
      'span',
      {},
      '是否删除培训：',
      h('span', { style: { color: '#09c690' } }, item.trainTitle)
    ),
    okText: '删除',
    okCancel: true,
    okButtonProps: { type: 'primary', danger: true },
    cancelButtonProps: { type: 'primary' },
    cancelText: '取消',
    onOk: async () => {
      await api.delPxgl({ id: item.trainId })
      message.success('删除成功')
      emits('reload')
      return Promise.resolve()
    },
    onCancel: () => {
      message.success('取消删除')
      return Promise.resolve()
    }
  })
}

provide('columns', columns.value)
</script>

<template>
  <!-- trainStatus -->
  <MyTable :pagination="pagination" :scroll="{ y: 'calc(100vh - 383px)' }">
    <template #bodyCell="{ column, record }">
      <template v-if="column.key === 'pickDate'">
        {{ record.startDate }} - {{ record.endDate }}
      </template>
      <template v-if="column.key === 'type'">
        {{ getConsants('banner_type', record.type) }}
      </template>
      <template v-if="column.key === 'trainType'">
        {{ getConsants('train_type', record.trainType) }}
      </template>
      <template v-if="column.key === 'trainStatus'">
        {{ getConsants('train_status', record.trainStatus) }}
      </template>
      <template v-if="column.key === 'trainImage' && record.trainImage">
        <ImageView :value="record.trainImage" />
      </template>
      <template v-if="column.key === 'trainContent'">
        <a-popover placement="left">
          <template #content>
            <div class="images-html" v-html="record.trainContent"></div>
          </template>
          <span style="color: #09c690">查看</span>
        </a-popover>
      </template>
      <template v-if="column.key === 'set'">
        <AButton type="link" @click="xq(record)">报名详情</AButton>
        <AButton type="link" @click="edit(record)">编辑</AButton>
        <AButton type="link" class="com-delbtn" @click="del(record)">删除</AButton>
      </template>
    </template>
  </MyTable>
</template>

<style lang="scss">
.images-html {
  width: 300px;
  img {
    max-width: 200px;
    max-height: 200px;
  }
}
</style>